package com.module.launcher.model

import cn.leancloud.LCObject
import cn.leancloud.LCQuery
import com.module.app.base.BaseViewModel
import com.module.base.cache.CacheSDK
import com.module.base.ext.request
import com.module.base.utils.LogUtils
import com.module.frame.ext.postEvent
import com.module.third.learcloud.lcQuerySingle
import kotlinx.coroutines.flow.MutableSharedFlow


class WelcomeViewModel : BaseViewModel() {

    /**
     * 配置flow
     */
    var configFlow: MutableSharedFlow<Boolean> = MutableSharedFlow()

    /**
     * 更新flow
     */
    var updateFlow: MutableSharedFlow<Boolean> = MutableSharedFlow()
    fun getConfig() {
        request({
            val tClass = com.module.app.bean.ConfigBean::class.java
            val query = LCQuery<LCObject>(tClass.simpleName)
            lcQuerySingle(query, tClass)
        }, {
            //成功
            it?.let {
                CacheSDK.put(com.module.app.bean.IType.ICache.CONFIG, it)
            }
            postEvent(updateFlow,true)
            postEvent(configFlow,true)

        }, {
            //失败
            LogUtils.d("配置信息请求失败：${it.code} ,${it.msg}")
            postEvent(configFlow,true)
        }, false, 3000)
    }
}